package com.qys.wallet.service.impl;

import com.qys.wallet.model.dto.CoinTypeSaveDTO;
import com.qys.wallet.model.entity.WalletCoinType;
import com.qys.wallet.mapper.WalletCoinTypeMapper;
import com.qys.wallet.pay.hyperbc.HyperbcService;
import com.qys.wallet.pay.hyperbc.dto.resp.CoinTypeChildDTO;
import com.qys.wallet.pay.hyperbc.dto.resp.CoinTypeDTO;
import com.qys.wallet.pay.hyperbc.dto.resp.ExchangeDTO;
import com.qys.wallet.service.WalletCoinTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2023-09-25
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class WalletCoinTypeServiceImpl extends ServiceImpl<WalletCoinTypeMapper, WalletCoinType> implements WalletCoinTypeService {

    private final HyperbcService hyperbcService;


    @Override
//    @PostConstruct
    public void sync() {
        log.info("同步币种开始");
        CoinTypeDTO coinTypeDTO = hyperbcService.supportedCoinType();
        List<CoinTypeChildDTO> list = coinTypeDTO.getList();
        for (CoinTypeChildDTO childDTO : list) {
            boolean flag = false;
            WalletCoinType coinType = baseMapper.selectById(childDTO.getCoin());
            if (coinType == null) {
                coinType = new WalletCoinType();
                coinType.setCreateTime(System.currentTimeMillis());
                flag = true;
            }
            coinType.setCoinName(childDTO.getCoin());
            coinType.setChainName(childDTO.getChain());
            coinType.setStatus(childDTO.getStatus());
            //币种兑换比例
            String coinName = coinType.getCoinName();
            if (coinName.contains("_")) {
                String[] s = coinName.split("_");
                coinName = s[0];
            }
            if (!"usdt".equals(coinName)) {
                try {
                    ExchangeDTO exchange = hyperbcService.exchange(coinName);
                    coinType.setExchange(new BigDecimal(exchange.getRate()));
                } catch (Exception e) {
                    coinType.setExchange(null);
                }
            } else {
                coinType.setExchange(BigDecimal.ONE);
            }
            if (flag) {
                log.info("初始化币种" + childDTO.getCoin());
                baseMapper.insert(coinType);
            } else {
                baseMapper.updateById(coinType);
            }
            saveOrUpdate(coinType);
        }
    }

    @Override
    public void putCoinType(CoinTypeSaveDTO coinTypeSaveDTO) {
        WalletCoinType coinType = baseMapper.selectById(coinTypeSaveDTO.getCoinName());
        if (null == coinType) {
            throw new RuntimeException("未查询到币种");
        }
        if (StringUtils.isNotEmpty(coinTypeSaveDTO.getCoinIcon())) {
            coinType.setCoinIcon(coinTypeSaveDTO.getCoinIcon());
        }
        if (null != coinTypeSaveDTO.getExchange()) {
            coinType.setExchange(coinTypeSaveDTO.getExchange());
        }
        baseMapper.updateById(coinType);
    }
}
